<?php

namespace zhtfrontend\modules\cardmanage\controllers;

use yii\console\Exception;
use yii\data\ActiveDataProvider;
use zhtfrontend\modules\cardmanage\components\rest\ActiveController;
use yii\helpers\ArrayHelper;
use yii\web\ForbiddenHttpException;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;

class SiteController extends ActiveController
{
    public $modelClass = 'common\models\datas\CardsIssuerinfor';

    public $serializer = [
        'class' => 'zhtfrontend\modules\cardmanage\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view'],$actions['login']);
        return $actions;
    }

    public function actionChangepass($id)
    {
        $data = \Yii::$app->request->post();
        $model = $this->findModel($id);
        if ($data['oldpass'] != null) {
            if (\Yii::$app->getSecurity()->validatePassword($data['oldpass'], $model->password)) {
                $model->password = \Yii::$app->security->generatePasswordHash($data['newpass']);
                $model->save(false);
                return $model;
            } else {
                try {
                    throw new ForbiddenHttpException('旧密码错误');
                } catch (ForbiddenHttpException $e) {
                    return ['status' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                }
            }
        }
    }

    public function actionLogin(){
        $data = \Yii::$app->request->get();
        $username = ArrayHelper::keyExists('username', $data) ? $data['username'] : '';
        $password = ArrayHelper::keyExists('password', $data) ? $data['password'] : '';
        if (empty($username) || empty($password)) {
            try {
                throw new ForbiddenHttpException('用户名或密码为空');
            } catch (ForbiddenHttpException $e) {
                return ['statusCode' => 0, 'message' => 'error', 'result' => $e->getMessage()];
            }
        }else{
            $modelClass = $this->modelClass;
            $model = $modelClass::find()->where(['or', "username=$username", "mobile=$username"])->one();
            if (empty($model)) {
                try {
                    throw new NotFoundHttpException('该用户不存在');
                } catch (NotFoundHttpException $e) {
                    return ['statusCode' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                }
            }else {
                if (empty($model->password)){
                    try {
                        throw new NotFoundHttpException('该用户未设置密码，请先设置密码');
                    } catch (NotFoundHttpException $e) {
                        return ['statusCode' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                    }
                }
                if (\Yii::$app->getSecurity()->validatePassword($password, $model->password)) {
                    $cookies = \Yii::$app->response->cookies;
                    $cookies->add(new \yii\web\Cookie([
                        'name' => 'user_id',
                        'value' => $model->id,
                        'expire' => time() + 6004800
                    ]));
                    return ['items'=>$model];
                } else {
                    try {
                        throw new ForbiddenHttpException('密码错误');
                    } catch (ForbiddenHttpException $e) {
                        return ['statusCode' => 0, 'message' => 'error', 'result' => $e->getMessage()];
                    }
                }
            }
        }
    }

}
